#!/bin/bash

# Set UTF-8 encoding
export LANG=en_US.UTF-8
export LC_ALL=en_US.UTF-8

echo "========================================"
echo "    Sokit Transfer Package Script"
echo "========================================"
echo

# Check if build directory exists
if [ ! -d "build" ]; then
    echo "[INFO] Creating build directory..."
    mkdir build
    if [ $? -ne 0 ]; then
        echo "[ERROR] Failed to create build directory!"
        exit 1
    fi
fi

# Enter build directory
echo "[INFO] Entering build directory..."
cd build

# Configure project
echo "[INFO] Configuring CMake project..."
cmake .. -DCMAKE_BUILD_TYPE=Release -DCMAKE_AUTOMOC=ON
if [ $? -ne 0 ]; then
    echo "[ERROR] CMake configuration failed!"
    echo "[HINT] Please check if Qt6 is properly installed"
    exit 1
fi

# Build project
echo "[INFO] Building project..."
make -j$(nproc)
if [ $? -ne 0 ]; then
    echo "[ERROR] Build failed!"
    echo "[HINT] Please check build environment and dependencies"
    exit 1
fi

# Check executable file
if [ ! -f "bin/sokit" ]; then
    echo "[ERROR] sokit executable not found"
    echo "[INFO] Searching for executable files..."
    find . -name "sokit" -type f 2>/dev/null
    if [ $? -ne 0 ]; then
        echo "[ERROR] No sokit executable files found"
        exit 1
    fi
fi

# Create package directory
echo "[INFO] Creating package directory..."
mkdir -p package/sokit
if [ $? -ne 0 ]; then
    echo "[ERROR] Failed to create package directory!"
    exit 1
fi

# Copy executable file
echo "[INFO] Copying executable file..."
cp bin/sokit package/sokit/
if [ $? -ne 0 ]; then
    echo "[ERROR] Failed to copy executable file!"
    exit 1
fi

# Copy other necessary files
echo "[INFO] Copying other files..."
if [ -f "../src/sokit/sokit.png" ]; then
    cp ../src/sokit/sokit.png package/sokit/
    echo "[INFO] Application icon copied"
fi
if [ -f "../README.md" ]; then
    cp ../README.md package/sokit/
    echo "[INFO] Documentation copied"
fi
if [ -f "../LICENSE" ]; then
    cp ../LICENSE package/sokit/
    echo "[INFO] License file copied"
fi

# Check if linuxdeployqt is available
if command -v linuxdeployqt &> /dev/null; then
    echo "[INFO] Using linuxdeployqt to create AppImage..."
    cd package/sokit
    linuxdeployqt sokit -appimage
    cd ../..
else
    echo "[WARNING] linuxdeployqt not found, skipping AppImage creation"
    echo "[HINT] You can manually download: https://github.com/probonopd/linuxdeployqt"
fi

# Check package result
echo "[INFO] Checking package result..."
if [ -f "package/sokit/sokit" ]; then
    echo "[SUCCESS] Executable file created"
else
    echo "[ERROR] Executable file creation failed"
    exit 1
fi

# Display package information
echo
echo "========================================"
echo "           Package Complete!"
echo "========================================"
echo
echo "[INFO] Package directory: $(pwd)/package/sokit"
echo "[INFO] Executable file: $(pwd)/package/sokit/sokit"
echo
echo "[HINT] You can copy the entire sokit folder to other computers"
echo "[HINT] Make sure target computers have necessary system dependencies"
echo
echo "[INFO] Package process completed!" 